package sample;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.MapValueFactory;

import java.util.List;
import java.util.Map;

public class Controller {
    @FXML
    private TreeView<String> schemaTreeView;
    @FXML
    private ListView<String> tableListView;
    @FXML
    private TextArea sqlTextArea;
    @FXML
    private TabPane resultTabPane;

    private String schema;
    private String table;

    @FXML
    void initialize() {
        assert schemaTreeView != null : "FXML file error!";
        assert tableListView != null : "FXML file error!";
        assert sqlTextArea != null : "FXML file error!";
        assert resultTabPane != null : "FXML file error!";

        configure();
    }

    private void configure() {
        TreeItem<String> root = new TreeItem<>("127.0.0.1");
        root.setExpanded(true);
        DBUtils.getSchema().stream().forEach(v -> {
            TreeItem<String> e = new TreeItem<>(v);
            root.getChildren().add(e);
        });
        schemaTreeView.setRoot(root);

        schemaTreeView.setOnMouseClicked(event -> {
            if (event.getClickCount() == 2) {
                schema = ((TreeView<String>) event.getSource()).getSelectionModel().getSelectedItem().getValue();
                tableListView.setItems(FXCollections.observableArrayList(DBUtils.getTable(schema)));
            }
        });

        tableListView.setOnMouseClicked(event -> {
            if (event.getClickCount() == 2) {
                table = ((ListView<String>) event.getSource()).getSelectionModel().getSelectedItem();

                TableView<Map<String, Object>> resultTableView = new TableView<>();
                List<String> columns = DBUtils.getColumn(schema, table);
                columns.stream().forEach(text -> {
                    TableColumn<Map<String, Object>, Object> tableColumn = new TableColumn<>(text);
                    tableColumn.setCellValueFactory(new MapValueFactory(text));
                    resultTableView.getColumns().add(tableColumn);
                });
                resultTableView.setItems(FXCollections.observableArrayList(DBUtils.getResult(schema, table, columns)));

                Tab tab = null;
                for (Tab item : resultTabPane.getTabs()) {
                    if (item.getText().equals(table)) {
                        tab = item;
                        break;
                    }
                }
                if (tab == null) {
                    tab = new Tab(table);
                    resultTabPane.getTabs().add(tab);
                }
                tab.setContent(resultTableView);
                resultTabPane.getSelectionModel().select(tab);
            }
        });
    }

    public void run(ActionEvent event) {
        String text = sqlTextArea.getText();
        if (text.isEmpty()) {
            new Alert(Alert.AlertType.NONE, "啥都没有，执行什么鬼？", ButtonType.OK).showAndWait();
            return;
        }
    }
}
